/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-19 14:32:08
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-19 16:43:42
 * @FilePath: /webServerAdmin/src/hooks/useSocket.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { io } from "socket.io-client";
import APIConfig from "@/config/config";
import { storeToRefs } from "pinia";
import { useUserStore } from "@/stores/modules/user";
import { unix } from "@/utils/date";
import md5 from "md5";

function useExtraHeaders() {
  const { token, userInfo } = storeToRefs(useUserStore());

  const timestamp = unix();
  return {
    authorization: `Bearer ${token.value}`,
    time: String(timestamp),
    uid: userInfo?.value?.id || "",
    sign: md5(`${md5(String(timestamp))}-${APIConfig.VITE_API_KEYS}`)
  };
}

export function useSocket(url) {
  return io(`${APIConfig.VITE_SOCKET_URL}${url}`, {
    reconnectionDelayMax: 10000,
    extraHeaders: useExtraHeaders()
  });
}
